const getUserInfo = () => {
  return new Promise((resolve, reject) => {
    wx.login({
      success(res) {
        wx.getUserInfo({
          withCredentials: true,
          lang: 'zh_CN',
          success: function(res1) {
            res1.userInfo.gender = res1.userInfo.gender == 1 ? 'MALE' : 'FEMALE';
            let param = res1.userInfo;
            param.iv = res1.iv;
            param.encryptedData = res1.encryptedData;
            param.code = res.code;
            param.avatar = res1.userInfo.avatarUrl;
            delete param.avatarUrl;
            resolve(param);
          }
        })
      }
    })
  })
}

const dealTime = (startTime, endTime) => {
  let val1 = new Date(startTime).valueOf() / 1000;
  let val2 = new Date(endTime).valueOf() / 1000;
  let diff = val2 - val1;
  let day = 0;
  let hour = 0;
  let minute = 0;
  if (diff > 86400) {
    day = parseInt(diff / 86400);
    diff = diff - day * 86400
  }
  if (diff > 3600) {
    hour = parseInt(diff / 3600);
    diff = diff - hour * 3600
  }
  if (diff > 60) {
    minute = parseInt(diff / 60);
    diff = diff - minute * 60
  } else {
    minute = 1
  }
  let str = `${day}天${hour}小时${minute}分钟`;
  return str;
}

const phoneSub = (phone) => {
  if (phone.length) {
    return phone.substr(0, 3) + "****" + phone.substr(7, 4)
  } else {
    return ''
  }
}

// 获取上个页面数据
const getPrevPage = function () {
  const currentPage = getCurrentPages()
  const prev = currentPage[currentPage.length - 2]
  return prev
}

const getValueByKey = function (e, key) {
  const dataset = e.currentTarget.dataset
  return dataset[key]
}
module.exports = {
  getUserInfo,
  dealTime,
  phoneSub,
  getPrevPage,
  getValueByKey
}